package services

import (
	"errors"
	"fmt"
	"hft/Database/admin"
	"hft/Http/Models/dao"
	utils "hft/Http/Utils"
	"hft/global"
	"hft/tools"
)

type FeedBack struct{}

// 个人的反馈列表
func (*FeedBack) GetAccountFeedBack(paginate *utils.Page, claims tools.BaseClaims) (list *utils.Page, err error) {

	var account *admin.Account

	var nowList []*admin.FeedBack

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return nil, errors.New("该账号不存在")
	}

	global.OC_DB.Scopes(dao.FeedBackDao.GetAccountFeedBackList(int(account.ID), paginate, global.OC_DB)).Find(&nowList).Scopes(dao.GetCount(global.OC_DB)).Count(&paginate.Total)

	paginate.Data = nowList

	return paginate, nil

}

func (*FeedBack) CreateFeedAccountBack(data *admin.FeedBack, claims tools.BaseClaims) (err error) {

	var account *admin.Account

	err = global.OC_DB.Scopes(dao.ParseAccountDao.Parse(claims, global.OC_DB)).First(&account).Error

	if err != nil {
		return errors.New("该账号不存在")
	}

	data.Status = 2
	data.AccountId = int(account.ID)
	data.RealName = account.RealName

	data.BelongName = utils.AccountBelongRegion(account.RegionLink, global.OC_DB)

	global.OC_DB.Create(&data)

	fmt.Println(tools.JsonParse(data))

	return nil

}

func (*FeedBack) GetAllFeedBack(paginate *utils.Page) (data *utils.Page, err error) {

	var list []*admin.FeedBack

	global.OC_DB.Scopes(dao.FeedBackDao.GetAllFeedBackList(paginate, global.OC_DB)).Find(&list).Scopes(dao.GetCount(global.OC_DB)).Count(&paginate.Total)

	paginate.Data = list

	return paginate, nil

}

func (*FeedBack) OperaFeedback(id int, status int) (err error) {

	err = global.OC_DB.Model(&admin.FeedBack{}).Where("id = ?", id).Update("status", status).Error

	if err != nil {
		return errors.New("未知错误500")
	}

	return nil

}

var FeedBackService = new(FeedBack)
